#include<bits/stdc++.h>
#define pii pair<long long,long long>
#define mp make_pair
#define pb push_back
using namespace std;
long long n,m,a[10005],k,ans,len,kn[100005][15],c[200005],ans1[200005],ra[200005];
string s[20];
void f(int dep,int wz,int wz1){
    memset(kn[wz1],0,sizeof(kn[wz1]));
    if(dep==0){
        if(wz<=len){
            kn[wz1][ra[wz]]=wz;
        }
        else{
            kn[wz1][14]=wz;
        }
    }
    else{
        int lt=wz1<<1,rt=wz1<<1|1;
        f(dep-1,(wz<<1)-1,lt);
        f(dep-1,(wz<<1),rt);
        if(s[dep][wz]=='1')swap(lt,rt);
        bool can=0;
        kn[wz1][14]=kn[lt][14];
        if(kn[lt][14])can=1;
        for(int i=0;i<dep;i++){
            if(kn[lt][i]){
                can=1;
            }
        }
        for(int i=dep;i<=13;i++){
            kn[wz1][i]+=kn[lt][i];
        }
        if(can){
            for(int i=0;i<=14;i++){
                kn[wz1][i]+=kn[rt][i];
            }
        }
        /*
        cout<<len<<" "<<dep<<" "<<wz<<"\n";
        for(int i=0;i<=18;i++){
            cout<<kn[wz1][i]<<" ";
        }
        cout<<"\n";//*/
    }
}
int main(){
    freopen("arena.in","r",stdin);
    freopen("arena.out","w",stdout);
    ios::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
    cin>>n>>m;
    k=log2(n-1)+1;
    for(int i=1;i<=n;i++){
        cin>>a[i];
    }
    for(int i=1;i<=m;i++){
        cin>>c[i];
    }
    for(int i=1;i<=k;i++){
        cin>>s[i];
        s[i]=" "+s[i];
    }
    long long T;
    cin>>T;
    while(T--){
        ans=0;
        long long x[4];
        for(int i=0;i<4;i++)cin>>x[i];
        for(int i=1;i<=n;i++)ra[i]=min(x[i&3]^a[i],13ll);
        ans1[1]=1;
        for(int i=2;i<=n;i++){
            len=i;
            int t=log2(i-1)+1;
            f(t,1,1);
            ans1[i]=0;
            for(int j=0;j<=14;j++){
                ans1[i]+=kn[1][j];
            }
        }
        for(int i=1;i<=m;i++){
            ans^=i*ans1[c[i]];
        }
        cout<<ans<<"\n";
    }
    return 0;
}
